﻿namespace Solver
{
	internal class Palindrome
	{
		public static bool IsPalindrome(int n, int @base)
		{
			var tmp = n;
			var reversed = 0;
			while (tmp > 0)
			{
				reversed = reversed * @base + tmp % @base;
				tmp /= @base;
			}
			return n == reversed;
		}

		public static bool IsPalindrome2(int n)
		{
			var tmp = n;
			var reversed = 0;
			while (tmp > 0)
			{
				reversed = (reversed << 1) + (tmp & 1);
				tmp >>= 1;
			}
			return n == reversed;
		}

		public static bool IsPalindrome10(int n)
		{
			return IsPalindrome(n, 10);
		}

		public static int Generate(int n, int @base, bool oddLength)
		{
			var p = n;
			if (oddLength) n /= @base;
			while (n > 0)
			{
				p = p * @base + n % @base;
				n /= @base;
			}
			return p;
		}
	}
}
